﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using log4net;
using log4net.Config;


namespace HttpWebRequestTest
{
    /// <summary>
    /// HttpWebRequest请求 WebService接口
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            HttpPost("http://10.99.101.28:90/SysBaseInfo.asmx/GetRolesInfo", "wyappuser", "1qaz2wsx");
            Exception ex = new Exception("调用出错了！");

            var log = LogManager.GetLogger("log");
            log.Info("信息：调用了！");

        }
        public static string HttpPost(string Url, string uid, string pwd)
        {
            try
            {
                string strData = "LoginId=" + uid + "&LoginPwd=" + pwd;
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = Encoding.UTF8.GetByteCount(strData);
                //request.CookieContainer = cookie;
                Stream myRequestStream = request.GetRequestStream();
                StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
                myStreamWriter.Write(strData);
                myStreamWriter.Close();

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                //response.Cookies = cookie.GetCookies(response.ResponseUri);
                Stream myResponseStream = response.GetResponseStream();
                StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
                string retString = myStreamReader.ReadToEnd();
                myStreamReader.Close();
                myResponseStream.Close();

                return retString;
            }
            catch (WebException ex)
            {
                HttpWebResponse response = (HttpWebResponse)ex.Response;
                Stream myResponseStream = response.GetResponseStream();
                StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
                string retString = myStreamReader.ReadToEnd();
                myResponseStream.Close();
                myStreamReader.Close();
                return ex.Message.ToString();
            }
        }

        public static string HttpPost()
        {
            try
            {
                RequestData data = new RequestData();
                data.LoginId = "wyappuser";
                data.LoginPwd = "1qaz2wsx";
                data.Data = new Data();
                data.Data.PersonCode = "L24147";
                string Json = JsonConvert.SerializeObject(data);

                //string Json = "LoginId=wyappuser&LoginPwd=1qaz2wsx&PersonCode=L24147";

                string url = "http://localhost:45308/LeDouServices.asmx/GetLeDouInfoByPersonCode";
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
                webRequest.Method = "POST";
                webRequest.KeepAlive = false;
                webRequest.ContentType = "application/x-www-form-urlencoded";
                byte[] buffer = Encoding.UTF8.GetBytes(Json);
                char[] chars = Encoding.UTF8.GetChars(buffer);
                webRequest.SendChunked = true;
                webRequest.TransferEncoding = "gzip, deflate, br";
                //webRequest.ContentLength = chars.Length;
                Stream stream = webRequest.GetRequestStream();
                StreamWriter sw = new StreamWriter(stream, Encoding.GetEncoding("gb2312"));
                sw.Write("RequestJson=" + Json);
                sw.Close();



                HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
                Stream ResponeS = webResponse.GetResponseStream();
                StreamReader sr = new StreamReader(ResponeS, Encoding.UTF8);
                string Res = sr.ReadToEnd();
                return "";

            }
            catch (Exception ex)
            {
                string mame = ex.GetType().Name;
                //Stream exstream = ex.Response.GetResponseStream();
                //StreamReader srex = new StreamReader(exstream);
                //string res = srex.ReadToEnd();
                throw;
                return "";
            }

        }
    }
}
